package ch.zbw.iCoach.withArrays;

import au.com.bytecode.opencsv.*;
import ch.zbw.iCoach.remo.*;
import java.io.*;
import java.util.*;

public class DataHandlingIO {

	LanguageList  myLang;
	CSVReader readerDE;
	CSVReader readerEN;
	CSVReader readerFR;
	CSVReader readerIT;
	CSVReader readerLang;
	CSVReader readerUser;

	CSVWriter writerLang;
	CSVWriter writerUser;

	String[] wordLang;
	String[] wordDE;
	String[] wordEN;
	String[] wordFR;
	String[] wordIT;
	String[] nameUser;

	String CsvPath = "./bin/";
	String CsvPathOS = "";
	String LangFileDE = "#DE.CSV";
	String LangFileEN = "#EN.CSV";
	String LangFileFR = "#FR.CSV";
	String LangFileIT = "#IT.CSV";
	String LangFile = "";
	String UserFile = "#User.CSV";

	/**
	 * Standard-Konstruktor
	 * 
	 * @return
	 */

	public DataHandlingIO(LanguageList languageList) {
		this.myLang = languageList;
	}
	
	public boolean startUp(String userName) {
		boolean errorIO = false;

		try {
			int langID, priKeyDE, parKeyDE, priKeyEN, parKeyEN, priKeyFR, parKeyFR, priKeyIT, parKeyIT; 
	           
            //System.out.println(CsvPath +LangFileDE);
            //Load CSV files
            readerDE = new CSVReader(new FileReader(CsvPath + LangFileDE));
            readerEN = new CSVReader(new FileReader(CsvPath + LangFileEN));
            readerFR = new CSVReader(new FileReader(CsvPath + LangFileFR));
            readerIT = new CSVReader(new FileReader(CsvPath + LangFileIT));
            
            //'delete' the header rows
            wordDE = readerDE.readNext();
            wordEN = readerEN.readNext();
            wordFR = readerFR.readNext();
            wordIT = readerIT.readNext();
            
            //FLUSH COMPLETE "DATABASE"
            myLang.clearAllData();
            
            while ((wordDE = readerDE.readNext()) != null)
            {
                //create categories
            	langID = Integer.parseInt(wordDE[0].trim());
                priKeyDE = Integer.parseInt(wordDE[1].trim());
                parKeyDE = Integer.parseInt(wordDE[2].trim());
                myLang.createLanguage(new Language (wordDE[3],langID, priKeyDE, parKeyDE,userName));

            }
	            
	            while ((wordEN = readerEN.readNext()) != null)
	            {
	                //create categories
	            	langID = Integer.parseInt(wordEN[0].trim());
	                priKeyEN = Integer.parseInt(wordEN[1].trim());
	                parKeyEN = Integer.parseInt(wordEN[2].trim());
	                myLang.createLanguage(new Language (wordEN[3],langID, priKeyEN, parKeyEN,userName));
	            }
	            
	            while ((wordFR = readerFR.readNext()) != null)
	            {
	                //create categories
	            	langID = Integer.parseInt(wordFR[0].trim());
	                priKeyFR = Integer.parseInt(wordFR[1].trim());
	                parKeyFR = Integer.parseInt(wordFR[2].trim());
	                myLang.createLanguage(new Language (wordFR[3],langID, priKeyFR, parKeyFR,userName));
	            }
	            
	            while ((wordIT = readerIT.readNext()) != null)
	            {
	                //create categories
	            	langID = Integer.parseInt(wordIT[0].trim());
	                priKeyIT = Integer.parseInt(wordIT[1].trim());
	                parKeyIT = Integer.parseInt(wordIT[2].trim());
	                myLang.createLanguage(new Language(wordIT[3],langID, priKeyIT, parKeyIT,userName));
	            }
	        }
		
			catch ( FileNotFoundException e )
			{
				System.err.println( "Files are not found: Go to the browser menu in Admin" );
				// e.printStackTrace();
		            errorIO = true;				
			}

	        catch(IOException e)
	        {
	            System.out.println("IOException: ");
	          //  e.printStackTrace();
	            errorIO = true;
	        }		
		        return errorIO;
	    }
		

	public boolean importLang(String CsvPathOS, String LangFile, String userName) {
		this.CsvPathOS = CsvPath;
		this.LangFile = LangFile;
		
		boolean errorIO = false;

		try {
			int langID, priKeyLang, parKeyLang;

			// Load CSV files
			readerLang = new CSVReader(new FileReader(CsvPath + LangFile));

			// 'delete' the header rows
			wordLang = readerLang.readNext();

			// FLUSH COMPLETE "Language Array"
			myLang.clearAllData();

			while ((wordLang = readerLang.readNext()) != null) {
				// create Languages
				langID = Integer.parseInt(wordLang[0].trim());
				priKeyLang = Integer.parseInt(wordLang[1].trim());
				parKeyLang = Integer.parseInt(wordLang[2].trim());
				myLang.addLanguage(new Language (wordLang[3],langID, priKeyLang, parKeyLang, userName));
			}
		}
		
		catch ( FileNotFoundException e )
		{
			System.err.println( "Files are not found: Go to the browser menu in Admin" );
			 e.printStackTrace();
	            errorIO = true;				
		}

		catch (IOException e) {
			System.out.println("IOException: ");
			e.printStackTrace();
			errorIO = true;
		}
		return errorIO;

	}

	public boolean importUserLang() {
		boolean errorIO = false;

		try {
			String firstName, lastName, userName;

			// Load CSV files
			readerUser = new CSVReader(new FileReader(UserFile));

			// 'delete' the header rows
			nameUser = readerUser.readNext();

			// FLUSH COMPLETE "Language Array"
			//myLang.clearAllData();

			while ((nameUser = readerUser.readNext()) != null) {
				firstName = (nameUser[0].trim());
				lastName = (nameUser[1].trim());
				userName = (nameUser[2].trim());
			}
		}
		
		catch ( FileNotFoundException e )
		{
			System.err.println( "Files are not found: Go to the browser menu in Admin" );
			 e.printStackTrace();
	            errorIO = true;				
		}

		catch (IOException e) {
			System.out.println("IOException: ");
			e.printStackTrace();
			errorIO = true;
		}
		return errorIO;

	}

	public boolean exportLang()
	    {
	        boolean errorIO = false;
	        
	        try
	        {
	        	generateNewLangPriKey();
	            
	            writerLang = new CSVWriter(new FileWriter(CsvPath+LangFile));
	            
	            String[] entriesLang = "WORD#LANGID#PRIMARY_KEY#PARENT_KEY".split("#");
	            
	            writerLang.writeNext(entriesLang);
	            
	            
	            for(int i = 0; i < myLang.getSize(); i++)
	            {
	            	Language tempLang = myLang.getlang(i);
	                entriesLang[i] = (tempLang.getWord().split("#") + "#" + tempLang.getLangID() +tempLang.getPrimaryKey()+ "#" + tempLang.getParentKey() );
	                writerLang.writeNext(entriesLang);
	                for(int j = 0; j < myLang.getSize(); j++)     {      
	           
	            writerLang.close(); 
		        }
	            }
	        }
	        catch(IOException e)
	        {
	            System.out.println("IOException: ");
	            e.printStackTrace();
	            errorIO = true;
	        }
	        return errorIO;
	    }

	public boolean exportUser(String firstName, String lastName, String userName)
    {
        boolean errorIO = false;
        
        try
        {
                        
            writerUser = new CSVWriter(new FileWriter(UserFile));
            
            String[] entriesUser = "firstName#lastName#userName".split("#");
            
            writerUser.writeNext(entriesUser);
            writerUser.close(); 
        }
        
        catch(IOException e)
        {
            System.out.println("IOException: ");
            e.printStackTrace();
            errorIO = true;
        }
        return errorIO;
    }
	

	public int generateNewLangPriKey() {
		int priKeyLang = myLang.getSize();
		int tempPriKeyLang = priKeyLang + 1;
		boolean PriKeyLangNotExist = true;
		
		for (int i = 0; i < myLang.getSize(); i++){
			Language tempLang = myLang.getlang(i);
			if (tempLang.getParentKey() == tempPriKeyLang){
				PriKeyLangNotExist = false;
			}
		}
		return tempPriKeyLang;
	}
	

}
		
		
		
		//for (int i = 0; i < myLang.getSize(); i++) {
			//Language tempLang = myLang.getlang(i);
			//tempLang.getPrimaryKey()getLanguage(i).setPirmaryKey(priKeyLang);
			//myLang.getLanguage(i).setParentKey(0);
			//for (int j = 0; j < myLang.getLanguage(i).getNumOfElements(); j++) {
				//myLang.getLanguage(i).setPirmaryKey(priKeyLang);
				//{
					//priKeyLang++;
				//}
			
